//
//  MKTitleOptionsTrackBar.m
//  MapleKit
//
//  Created by 赵磊 on 2019/7/25.
//

#import "MKTitleOptionsTrackBar.h"

@interface MKTitleOptionsTrackBar ()

///上次点击的按钮
@property (nonatomic,weak) UIButton *lastButton;

@end

@implementation MKTitleOptionsTrackBar

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.alwaysBounceVertical = NO;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
    }
    return self;
}

#pragma mark - Set
- (void)setTitles:(NSArray<NSString *> *)titles {
    if (self.titles) {
        return;
    }
    _titles = titles;
    [self createItems];
}
- (void)setSelectedTitleFont:(UIFont *)selectedTitleFont {
    _selectedTitleFont = selectedTitleFont;
    [self updateItems];
}
- (void)setSelectedTitleColor:(UIColor *)selectedTitleColor {
    _selectedTitleColor = selectedTitleColor;
    [self updateItems];
}
- (void)setUnSelectedTitleFont:(UIFont *)unSelectedTitleFont {
    _unSelectedTitleFont = unSelectedTitleFont;
    [self updateItems];
}
- (void)setUnSelectedTitleColor:(UIColor *)unSelectedTitleColor {
    _unSelectedTitleColor = unSelectedTitleColor;
    [self updateItems];
}
- (void)setTrackBarColor:(UIColor *)trackBarColor {
    _trackBarColor = trackBarColor;
    [self updateItems];
}
- (void)setTrackBarBottomSpace:(CGFloat)trackBarBottomSpace {
    _trackBarBottomSpace = trackBarBottomSpace;
    [self updateItems];
}
- (void)setSelectIndex:(NSInteger)selectIndex {
    //防越界处理
    if (selectIndex >= self.contentView.subviews.count) {
        return;
    }
    self.lastButton.selected = NO;
    self.lastButton = self.contentView.subviews[selectIndex];
    self.lastButton.selected = YES;
    [self updateItems];
}

#pragma mark - Private
- (void)createItems {
    //如果接收到的是空值，释放元素
    if (!self.titles || !self.titles.count) {
        [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.trackBar removeFromSuperlayer];
        self.trackBar = nil;
        return;
    }
    //布局元素
    for (UIView *view in self.contentView.subviews) {
        view.hidden = YES;
    }
    for (NSInteger index = 0; index < self.titles.count; index++) {
        NSString *title = self.titles[index];
        UIButton *button = [[UIButton alloc] init];
        [button addTarget:self action:@selector(itemAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        [button setTitle:title forState:UIControlStateNormal];
        button.frame = CGRectMake(self.contentView.frame.size.width / self.titles.count * index, 0, self.contentView.frame.size.width / self.titles.count, self.frame.size.height);
        if (!index) {//标记
            button.selected = YES;
            self.lastButton = button;
        }else if (index == self.titles.count - 1) {
            self.contentSize = CGSizeMake(CGRectGetMaxX(button.frame), self.frame.size.height);
        }
    }
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake((self.contentView.frame.size.width / self.titles.count - 25.0) / 2, self.frame.size.height - 5.0, 25.0, 2.0);
    [self.contentView.layer addSublayer:layer];
    self.trackBar = layer;
    [self updateItems];
}
- (void)updateItems {
    //配置默认的属性值
    if (!self.trackBarBottomSpace) {
        self.trackBarBottomSpace = 3.0;
    }
    if (!self.trackBarColor) {
        self.trackBarColor = [UIColor colorWithWhite:51 / 255.0 alpha:1.0];
    }
    if (!self.unSelectedTitleFont) {
        self.unSelectedTitleFont = [UIFont systemFontOfSize:15.0];
    }
    if (!self.selectedTitleFont) {
        self.selectedTitleFont = [UIFont boldSystemFontOfSize:15.0];
    }
    if (!self.unSelectedTitleColor) {
        self.unSelectedTitleColor = [UIColor colorWithWhite:153 / 255.0 alpha:1.0];
    }
    if (!self.selectedTitleColor) {
        self.selectedTitleColor = [UIColor colorWithWhite:51 / 255.0 alpha:1.0];
    }
    //修改成最新的值
    self.trackBar.backgroundColor = self.trackBarColor.CGColor;
    self.trackBar.frame = CGRectMake(self.trackBar.frame.origin.x, self.frame.size.height - self.trackBarBottomSpace - self.trackBar.frame.size.height, self.trackBar.frame.size.width, self.trackBar.frame.size.height);
    for (UIButton *sender in self.contentView.subviews) {
        [sender setTitleColor:self.unSelectedTitleColor forState:UIControlStateNormal];
        [sender setTitleColor:self.selectedTitleColor forState:UIControlStateSelected];
        sender.titleLabel.font = sender.selected ? self.selectedTitleFont : self.unSelectedTitleFont;
        if (sender.selected) {
            self.trackBar.frame = CGRectMake(CGRectGetMinX(sender.frame) + (CGRectGetWidth(sender.frame) - self.trackBar.frame.size.width) / 2, self.frame.size.height - self.trackBarBottomSpace - self.trackBar.frame.size.height, self.trackBar.frame.size.width, self.trackBar.frame.size.height);
        }
    }
}

#pragma mark - Action
- (void)itemAction:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    //更新状态
    sender.selected = !sender.selected;
    self.lastButton.selected = !self.lastButton.selected;
    self.lastButton = sender;
    //更新属性
    [self updateItems];
    //传递事件
    if (self.action) {
        self.action([self.contentView.subviews indexOfObject:sender]);
    }
}

@end
